﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace MvcSample.Validations
{
    public class LengthAttribute
        : ValidationAttribute
    {
        public LengthAttribute()
        { }

        public LengthAttribute(Func<string> errorMessageAccessor)
            : base(errorMessageAccessor)
        { }

        public LengthAttribute(string errorMessage)
            : base(errorMessage)
        { }

        public override bool IsValid(object value)
        {
            return base.IsValid(value);
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value==null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            var str = value.ToString();
            if (str.Length<6)
            {
                return new ValidationResult("长度不够");
            }
            return new ValidationResult("长度不够");
        }
    }
}
